package club.dataresources.service.impl;

import club.dataresources.entity.*;
import club.dataresources.mapper.MenuMapper;
import club.dataresources.mapper.ProductMenuDetailMapper;
import club.dataresources.mapper.ServiceMenuDetailMapper;
import club.dataresources.service.MenuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author
 * @create 2018-11-07 23:52
 **/
@Service
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private ProductMenuDetailMapper productMenuDetailMapper;

    @Autowired
    private ServiceMenuDetailMapper serviceMenuDetailMapper;

    @Override
    public Page<Menu> listMenu(Integer page, Integer rows,Menu menu) throws Exception {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();

        if(menu.getMenuBy()!=null){
            criteria.andMenuByEqualTo(menu.getMenuBy());
        }else{
            //所有menuBy为0的是管理员添加的套餐
            criteria.andMenuByEqualTo(0);
        }

        if(menu.getMenuName()!=null){
            criteria.andMenuNameLike(menu.getMenuName());
        }
        if(menu.getMenuDescription()!=null){
            criteria.andMenuDescriptionLike(menu.getMenuDescription());
        }

        criteria.andMenuIdIsNotNull();
        return menuMapper.selectByExample(example);
    }

    @Override
    public HashMap<String, Object> detailMenuById(Integer menuId) throws Exception {
        //点击套餐具体显示套餐内容，根据套餐id
        HashMap<String,Object> map = new HashMap<String,Object>();
        ProductMenuDetailExample example1 = new ProductMenuDetailExample();
        ProductMenuDetailExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andMenuIdEqualTo(menuId);
        List<ProductMenuDetail> liP =  productMenuDetailMapper.selectByExample(example1);

        ServiceMenuDetailExample example2 = new ServiceMenuDetailExample();
        ServiceMenuDetailExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andMenuIdEqualTo(menuId);
        List<ServiceMenuDetail> liS = serviceMenuDetailMapper.selectByExample(example2);

        map.put("liP",liP);
        map.put("liS",liS);
        return map;
    }

    @Override
    public Menu getMenuById(Integer menuId) throws Exception {
        return menuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public List<Menu> findMenuByMenuBy(Integer menuBy) throws Exception {
        return menuMapper.findMenuByMenuBy(menuBy);
    }

    @Override
    public boolean addMenu(Menu menu) throws Exception {
        return menuMapper.insertSelective(menu)==1;
    }

    @Override
    public boolean removeMenu(Integer menuId) throws Exception {
        return menuMapper.deleteByPrimaryKey(menuId)==1;
    }

    @Override
    public boolean updateMenu(Menu menu) throws Exception {
        return menuMapper.updateByPrimaryKeySelective(menu)==1;
    }

    @Override
    public boolean addAndReturnId(Menu menu) throws Exception {
        return menuMapper.addAndReturnId(menu)==1;
    }
}
